﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Diagnostics;
using System.Windows.Threading;
using VanaLib;

namespace VanaVista
{

	public partial class Window1
	{
        private DispatcherTimer timer;
        private Canvas[] elWeek = new Canvas[8];
        private Canvas[] elWeekWeak = new Canvas[8];
        private int lastWeek =0;
        private double[] elWeekLeft = new double[8]; 

        public Window1()
		{
			this.InitializeComponent();
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(OnTimedEvent);
			
			// Insert code required on object creation below this point.
		}

        private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // 曜日要素の初期化
            elWeek[0] = 火曜日; elWeekWeak[0] = 氷曜日;
            elWeek[1] = 土曜日; elWeekWeak[1] = 雷曜日;
            elWeek[2] = 水曜日; elWeekWeak[2] = 火曜日;
            elWeek[3] = 風曜日; elWeekWeak[3] = 土曜日;
            elWeek[4] = 氷曜日; elWeekWeak[4] = 風曜日;
            elWeek[5] = 雷曜日; elWeekWeak[5] = 水曜日;
            elWeek[6] = 光曜日; elWeekWeak[6] = 闇曜日;
            elWeek[7] = 闇曜日; elWeekWeak[7] = 光曜日;
            for (int i = 0; i < elWeek.Length; i++) elWeekLeft[i] = Canvas.GetLeft(elWeek[i]);

            
            // 初回表示
            OnTimedEvent(sender, null);
        }

        private void OnTimedEvent(object source, EventArgs e)
        {
            Debug.WriteLine("OnTimedEvent:");
            timer.Stop();

            // 描画
            long nowTick = DateTime.UtcNow.Ticks;
            update(nowTick);

            // タイマー再設定
            setInterval();
        }

        private void setInterval()
        {
            long nowTick = DateTime.UtcNow.Ticks;
            long nextTick = VanaLib.VanaTime.CalcNextTick(nowTick);
            long span = nextTick - nowTick;
            if (span < 1) span = 1;
            timer.Interval = new TimeSpan(1);
            timer.Interval = new TimeSpan(span);
            timer.Start();
        }


        private void update(long utcTick)
        {
            // ローカル日時表示
            DateTime utc = new DateTime(utcTick);
            DateTime local = utc.ToLocalTime();
            地球時刻.Content = local.ToString("HH:mm:ss");

            // ヴァナ時間
            VanaTime vt = new VanaTime(utcTick);
            ヴァナ時刻.Content =vt.ToTimeString();

            // ヴァナ日付
            ヴァナ日付.Content =vt.ToDateString();

            // ヴァナ曜日
            setWeek(vt.DayOfWeek);

        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            setWeek(lastWeek + 1);
        }

        private void setWeek(int week)
        {
            week %= elWeek.Length;
            if (lastWeek == week) return;
            lastWeek = week;

            // 曜日マーク移動アニメーション
            Storyboard story = new Storyboard();
            int pos = week-1;
            for (int i = 0; i < elWeek.Length; i++) {
                pos++;
                pos %= elWeek.Length;
                DoubleAnimation animate = new DoubleAnimation();
                animate.From =Canvas.GetLeft(elWeek[pos]);
                animate.To =elWeekLeft[i];
                animate.Duration = new Duration(TimeSpan.FromSeconds(0.3));
                animate.AccelerationRatio = 0.0;
                animate.DecelerationRatio = 1.0;
                if (i == elWeek.Length - 1) animate.BeginTime = TimeSpan.FromSeconds(0.2);

                story.Children.Add(animate);
                Storyboard.SetTargetName(animate, elWeek[pos].Name);
                Storyboard.SetTargetProperty(animate, new PropertyPath(Canvas.LeftProperty));

                Canvas.SetZIndex(elWeek[pos], 20 - i);
            }
            story.Begin(this);

            // 弱属性表示切替
            Ellipse ellipseWeak = (Ellipse)elWeekWeak[week].Children[0];
            TextBlock textWeak = (TextBlock)elWeekWeak[week].Children[1];
            弱属性曜日枠.Fill = ellipseWeak.Fill;
            弱属性曜日枠.Stroke = ellipseWeak.Stroke;
            弱属性曜日テキスト.Text = textWeak.Text;
            弱属性曜日テキスト.Foreground = textWeak.Foreground;
        }


    }

}